package com.wolfking.jeesite.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wolfking.jeesite.entity.User;
import com.wolfking.jeesite.entity.UserRoleRelation;
import com.wolfking.jeesite.exception.JeesiteException;
import com.wolfking.jeesite.mapper.UserMapper;
import com.wolfking.jeesite.mapper.UserRoleRelationMapper;
import com.wolfking.jeesite.mybatis.BaseService;
import com.wolfking.jeesite.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

import static com.wolfking.jeesite.exception.ExceptionCode.BIZ_ERROR_USER_COUNT_NAME_NOMATHCH;

/**
 * 用户的服务
 *
 * @author wolfking@赵伟伟
 * @mail zww199009@163.com
 * @创作日期 2017年4月23日 上午11:00:14
 * @copyright wolfking
 */
@Service
public class UserService extends BaseService<User> {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleRelationMapper userRoleRelationMapper;

    /**
     * 根据角色ID查询用户
     *
     * @param roleId 角色ID
     * @return 用户列表
     */
    @Transactional(readOnly = true)
    public List<User> getUserByRoleId(Integer roleId) {
        return userMapper.getUserByRoleId(roleId);
    }

    /**
     * 获取用户所有的鉴权码
     *
     * @param userId
     * @return
     */
    @Transactional(readOnly = true)
    public List<String> getUserAllAuthCodes(Integer userId) {
        return userMapper.getUserAllAuthCodes(userId);
    }


    @Transactional
    public void modifyPwd(Integer id, String oldPassword, String newPassword) {
        User user = getById(id);
        if (!PasswordUtil.validatePassword(oldPassword, user.getPassword()))
            throw new JeesiteException(BIZ_ERROR_USER_COUNT_NAME_NOMATHCH.value(), "原始密码不对");
        user.setPassword(PasswordUtil.entryptPassword(newPassword));
        super.updateById(user);
    }

    @Override
    @Transactional
    public List<User> deleteById(Serializable id) {
        final List<User> users = super.deleteById(id);
        if (users.size() > 0) {
            LambdaQueryWrapper<UserRoleRelation> wrapperUr = new QueryWrapper<UserRoleRelation>()
                    .lambda().eq(UserRoleRelation::getUserId, id);
            userRoleRelationMapper.delete(wrapperUr);
        }
        return users;
    }

    @Override
    @Transactional
    public boolean save(User user) {
        boolean add = super.save(user);
        if (add && user.getRoleIdList() != null) {
            for (Integer roleId : user.getRoleIdList()) {
                UserRoleRelation relation = new UserRoleRelation();
                relation.setUserId(user.getId());
                relation.setRoleId(roleId);
                userRoleRelationMapper.insert(relation);
            }
        }
        return add;
    }

    @Override
    @Transactional
    public boolean updateById(User user) {
        boolean update = super.updateById(user);
        if (update) {
            LambdaQueryWrapper<UserRoleRelation> wrapperUr = new QueryWrapper<UserRoleRelation>()
                    .lambda().eq(UserRoleRelation::getUserId, user.getId());
            userRoleRelationMapper.delete(wrapperUr);
            if (Objects.nonNull(user.getRoleIdList())) {
                for (Integer roleId : user.getRoleIdList()) {
                    UserRoleRelation relation = new UserRoleRelation();
                    relation.setUserId(user.getId());
                    relation.setRoleId(roleId);
                    userRoleRelationMapper.insert(relation);
                }
            }
        }
        return update;
    }

    @Transactional
    public boolean updateUserInfo(User user) {
        return super.updateById(user);
    }
}
